package lu.med.praxiswahl.wahlumed.client.kalender;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;

/**
 * 
 * @author Lars Holger Roth
 */
public class KalenderCellRenderer implements TableCellRenderer {

	private Color black = new Color(0, 0, 190);
	private Color white = new Color(255, 255, 255);
	private Color red = new Color(255, 0, 0);
	private Color grey = new Color(200, 200, 200);
	private Color yellow = new Color(200, 200, 0);
	private Font bold = new Font("Arial", Font.BOLD, 12);
	private Color blue = new Color(0, 0, 250);
	private Color green = new Color(0, 200, 0);
	private KalenderTableDataModel model;

	public KalenderCellRenderer(KalenderTableDataModel model) {
		this.model = model;
	}

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		// Wenn die Menge des Artikels knapp ist, so wird die entsprechende
		// Zeile Wei� auf Rot dargestellt, ansonsten Schwarz auf Wei�
		JLabel label = new JLabel();

		label.setOpaque(true);
		Border b = BorderFactory.createEmptyBorder(1, 1, 1, 1);
		label.setBorder(b);
		label.setFont(table.getFont());

		if ((row % 4 == 0) || (row == 0)) {
			label.setForeground(table.getForeground());
			label.setBackground(grey);
		} else {
			// Jede zweite Zeile bekommt grauen Hintergrund
			label.setForeground(table.getForeground());
			label.setBackground(table.getBackground());
		}

		if (column == 7) {
			// Wartezeit gr��er 30 rot f�rben
			String w = label.getText();
			int index = w.indexOf("min");
			if (index != -1) {
				w = w.substring(0, index);
				try {
					index = Integer.parseInt(w);
				} catch (NumberFormatException e) {
					index = 0;
				}
				if (index > 30) {
					label.setForeground(red);
				}
			}
		}

		// Die 13 Uhr Spalte wird grün
		if (row == 20) {
			label.setBackground(green);
		}

		// Sonderf�lle f�r die Farben
		if (model == null) {
			label.setText("");
		} else {
			label.setText((String) model.getValueAt(row, column));

			if ((column == 3) && (label.getText() != null)) {
				// Farben für den US Typ
				if (label.getText().equals("1.GUS")) {
					label.setBackground(yellow);
				} else if (label.getText().equals("AC")) {
					label.setBackground(red);
					label.setForeground(white);
					label.setFont(bold);
				} else if (label.getText().equals("AC+FISH")) {
					label.setBackground(red);
					label.setForeground(white);
					label.setFont(bold);
				} else if (label.getText().equals("IUP")) {
					label.setBackground(red);
					label.setForeground(white);
					label.setFont(bold);
				} else if (label.getText().equals("AC (Homburg)")) {
					label.setBackground(red);
					label.setForeground(white);
					label.setFont(bold);
				} else if (label.getText().equals("Kein OP")) {
					label.setBackground(red);
					label.setForeground(white);
					label.setFont(bold);
				} else if (label.getText().equals("URG")) {
					label.setBackground(red);
					label.setForeground(white);
					label.setFont(bold);
				}

				else if (label.getText().equals("NUS")) {
					label.setBackground(yellow);
				} else if (label.getText().equals("IOP")) {
					label.setBackground(yellow);
				} else if (label.getText().toLowerCase().equals("pharma")) {
					label.setBackground(green);
					label.setFont(bold);
					label.setForeground(white);
				}
			} else if ((column >= 9) && (label.getText() != null)) {
				if ((column == 9) && (label.getText().equals("H"))) {
					label.setForeground(blue);
					label.setFont(bold);
				} else if (column == 10) {
					label.setForeground(blue);
				}
			}
		}

		if (column >= 9) {
			label.setHorizontalAlignment(SwingConstants.CENTER);
		}

		if (hasFocus) {
			label.setBackground(black);
			label.setForeground(white);
		} else {
			// Angezeigte Spalte in Modellspalte umwandeln
			column = table.convertColumnIndexToModel(column);
		}

		// Falls es eine neue Patientin ist, so muss ein Extra Panel
		// erstellt werden. In jenes wird ein grüner Punkt eingesezeichnet
		if (model.isNewPatient(row) && (column == 1)) {
			// Es handelt sich um eine neue Patientin
			return new NewPatientTableRowPanel(label);
		}

		label.setToolTipText(label.getText());
		return label;
	}
}
